package com.zjff.show.zjffshow.controller;

import com.zjff.show.zjffshow.entity.*;
import com.zjff.show.zjffshow.service.InfoService;
import com.zjff.show.zjffshow.tool.JSONTool;
import com.zjff.show.zjffshow.tool.RedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Info/")
public class InfoController {

    @Autowired
    InfoService infoService;
    ResultDao resultDao = null;
    Jedis jedis = new RedisTool().getJedis();

    @RequestMapping(value = "/getInfoList",method = RequestMethod.GET)
    private String getInfoList(HttpServletRequest request){
        resultDao = new ResultDao();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        String time = request.getParameter("time");
        String content = request.getParameter("content");
        int intid = 0;
        boolean flag = false;
        if (id!=null && !id.equals("") && !id.equals("0")){
            intid = Integer.parseInt(id);
            flag = true;
        }
        if (!time.equals("")){
            flag = true;
        }
        if (!content.equals("")){
            flag = true;
        }
        int Start=(page-1)*limit;//设置起始位置
        List<InfoDao> list = infoService.getInfoList(intid,Start,limit,time,content);
        resultDao.setCode("0");
        resultDao.setMsg("获取成功！");
        if (flag){
            resultDao.setCount(String.valueOf(list.size()));
        }else {
            resultDao.setCount(String.valueOf(infoService.getInfoCount()));
        }
        resultDao.setData(list);
        return JSONTool.getJsonTool().toJson(resultDao);
    }

    //获取所有用户数量
    @RequestMapping(value = "/getInfoCount",method = RequestMethod.GET)
    private String getInfoCount(HttpServletRequest request){
        resultDao = new ResultDao();
        int count = infoService.getInfoCount();
        resultDao.setCount(String.valueOf(count));
        return JSONTool.getJsonTool().toJson(resultDao);
    }





}
